/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.common.service.serviceImpl;

import com.bjboot.caocao.system.config.BJBootSysConfigParam;
import com.bjboot.caocao.system.dao.FileMapper;
import com.bjboot.caocao.system.domain.FileDO;
import com.bjboot.caocao.common.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 *@Description FileService实现类
 *@Author 操文健
 *@Date 2018-09-10 上午10:53
 *@Version 1.0
 **/
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private BJBootSysConfigParam bjBootSysConfigParam;

    @Override
    public FileDO get(Long id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        } else {
            return fileMapper.selectByPrimaryKey(id);
        }
    }

    @Override
    public List<FileDO> list(Map<String, Object> map) {
        return fileMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return fileMapper.count(map);
    }

    @Override
    public int save(FileDO sysFile) {
        return fileMapper.insertSelective(sysFile);
    }

    @Override
    public int update(FileDO sysFile) {
        return fileMapper.updateByPrimaryKeySelective(sysFile);
    }

    @Override
    public int remove(Long id) {
        return fileMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return fileMapper.batchRemove(ids);
    }

    @Override
    public Boolean isExist(String url) {
        Boolean isExist = false;
        if (!StringUtils.isEmpty(url)) {
            String filePath = url.replace("/files/", "");
            filePath = bjBootSysConfigParam.getUploadPath() + filePath;
            File file = new File(filePath);
            if (file.exists()) {
                isExist = true;
            }
        }
        return isExist;
    }
}
